/*
  学习目标：数据存入redux， 渲染列表
  1. actionTYpe文件中定义type常量
  2. dispatch(actin对象)
  3. reducer完成计算
*/
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getNewsAction } from '../store/action/news';

import defaultSrc from '../assets/defaultCover.jpg';

export default function News() {
  const dispatch = useDispatch();
  const { current } = useSelector((state) => state.channel);
  // 4. 取出数据
  const { newsList } = useSelector((state) => state.news);

  useEffect(() => {
    dispatch(getNewsAction(current));
  }, [dispatch, current]);

  return (
    <div className="list">
      {newsList.map((item) => {
        return (
          <div key={item.art_id} className="article_item">
            <h3 className="van-ellipsis">{item.title}</h3>
            <div className="img_box">
              <img
                src={item.cover.type === 0 ? defaultSrc : item.cover.images[0]}
                className="w100"
                alt=""
              />
            </div>
            <div className="info_box">
              <span>{item.aut_name}</span>
              <span>{item.comm_count}评论</span>
              <span>{item.pubdate}</span>
            </div>
          </div>
        );
      })}
    </div>
  );
}
